/**
 *
 *
 * TOOD
 * 一个心跳内，for 所有队列，无论队列什么状态，都要遍历
 * 取出队列中需要战斗的阵营中的需要战斗的玩家，执行一次攻击逻辑和输出战斗一次战斗效果
 * @author Miek/Fang.j/大当家
 * @type {string[]}
 */


//载入那些函数库
let includes = ([
    "include/common.js",
]);
//继承哪些JS 在所有代码之前要完成继承
let inherits = ([
    "inherit/data.js",
]);


let campData = {

    name: "阵营名称",
    //所在队列
    que: 0,
    //阵营中的杀手
    killers: [],
    //敌方阵营
    enemyCamp: 0,
    //1等待战斗 2战斗中 3 成员全部战斗完毕
    stat: 1,
}

//状态标记
let FIX_STAT = {
    QUE: {
        WAIT: 1,//等待战斗
        FIGHTING: 2,//战斗中
        COMLPLETE: 3,//战斗完成
        WAIT_DEL: 4,//所有队列的成员全部战斗完成
    },
    CAMP: {
        WAIT: 1,
        //已战斗
        HASCOMBAT: 2,
        //成员全部战斗完毕
        COMLPLETE: 3,
        //已经没有可在战斗的杀手
        NOKILLER: 4,
    }
}

//战斗队列  存储多个阵营，最多3个
let combatGroupQue;// = [
//     // {
//     //     name: "队列名字-通常以激活战斗的玩家的名字为主",
//     //     camps: [],
//     //     stat: FIX_STAT.QUE.WAIT
//     // }
// ];


//初始状态
let hasInit = false;


//被创建的时候触发
function create() {
    let me = thisObject();
    me.set("channelId", "修真回合游戏");
    return me;
}


/**
 * 获取战斗队列下的需要战斗的阵营的杀手
 */
function getNeedCombatCampKiller(que) {

    let me = thisObject();
    let findCamps = que.camps;
    if (que.idx >= que.camps.length) {
        que.idx = 0;
    }
    let camp = que.camps[que.idx];
    que.idx++;
    return camp;
}


/**
 *
 */
function getWinAndLose(camp1, camp2) {

    let data = {
        win: 0,
        lost: 0,
    };

    let c1 = camp1.killers.filter((e) => {
        if (e.get("hp") > 0) {
            return e;
        }
    });

    let c2 = camp2.killers.filter((e) => {
        if (e.get("hp") > 0) {
            return e;
        }
    });

    //判断平局,双方都还有杀手。。。
    if (c1.length > 0 && c2.length > 0) {
        return 1;
    }

    if (c1.length > c2.length) {
        data.win = camp1;
        data.lost = camp2;
    } else {
        data.win = camp2;
        data.lost = camp1;
    }
    return data;
}


/**
 * 添加战斗阵营到战斗队列
 * @param que
 * @param camp
 */
function addCampTo(camps) {
    let me = thisObject();
    camps.forEach(e => {
        me.combatGroupQue.push(e);
    })
}


/**
 * 添加杀手到阵营
 * @param camp
 * @param killers
 */
function addKillerTo(camp, killers) {
    let me = thisObject();
    killers.forEach(e => {
        me.fightingCamps.push(e);
    })
}


/**
 * 找出阵营中可战斗的杀手，随机找一个
 * @param camp
 * @returns {boolean|*}
 */
function getKillerRandom(camp) {

    let me = thisObject();
    let killers = camp.killers;
    let okKillers = killers.filter((e) => {
        if (e && e.get("hp") > 0) {//还有血量的...
            return e;
        }
    });
    if ( okKillers.length <= 0) {
        os.debug("没有被杀者了")
        return false;
    }
    okKillers = getRandomOneOfArr(okKillers);
    //该阵营已无人可战
    return okKillers;
}

/**
 * 按占位顺序找出杀手
 * 需要考虑杀手是否还有血量
 * @param camp
 * @returns {*|boolean}
 */
function getKiller(camp) {

    let me = thisObject();

    let okKillers = camp.killers.filter((e) => {
        if (e && e.get("hp") > 0 && !e.get("hasFight")) {//还有血量的...
            return e;
        }
    });

    if (okKillers.length <= 0) {
        //os.debug("没有杀手可用了")
        return false;
    }
    okKillers[0].set("hasFight", true);
    return okKillers[0];
}

function resetAttacks(camp1, camp2) {

    camp1.killers.forEach((e) => {
        e.set("hasFight", false);
    });
    camp2.killers.forEach((e) => {
        e.set("hasFight", false);
    });
}


/**
 * 找出可战斗的阵营
 * @returns {boolean|*}
 */
function getNeedFigthCamp() {
    let me = thisObject();
    //每一轮只有一阵营在战斗
    for (let k in me.fightingCamps) {
        //选择可战斗的
        let camp = me.fightingCamps[k];
        //取出么有战斗过的...
        os.debug()
        if (camp.stat == 1 && me.getKiller(camp)) {//等待战斗的
            return camp;
        }
    }
    return false;
}


function combat(killer, victim) {


    let sk = killer.rdDynamicSkill();


    os.debug(sk.name, "杀手技能");

    //计算攻击
    let damage = killer.getAttack() - victim.getDefense();
    let victimHp = victim.get("hp");
    os.debug(damage, "杀手的攻击力");
    if (victimHp <= 0) {
        os.debug("对方都死了，还是别打了!");
        return;
    }

    if (damage > 0 && victimHp > 0) {
        victim.set("hp", victimHp - damage);
    }

    os.debug(victim.get("hp"), "被杀者血量");


}


//测试使用
function init() {

    let me = thisObject();

    let usr = newObj("clone/user.js");

    let usr2 = newObj("clone/user.js");

    let usr3 = newObj("clone/user.js");

    let usr4 = newObj("clone/user.js");

    usr.set("name", "张三丰")
    usr.set("id", "zhangsanfeng")
    usr.set("hp", 100);
    usr.set("defense", 5);

    usr.setTemp("rdIdx", 1);

    usr2.set("name", "逍遥子")
    usr2.set("id", "xiaoyaozi")
    usr2.set("hp", 100);
    usr2.set("defense", 5);

    usr2.setTemp("rdIdx", 2);

    usr.set("rdSkills", [
        100001, 100002
    ]);


    usr2.set("rdSkills", [
        100068, 100076
    ]);

    usr3.set("name", "姜子牙")
    usr3.set("id", "jiangziya")
    usr3.set("hp", 100);
    usr3.set("defense", 5);
    usr3.setTemp("rdIdx", 1);
    //-----------------------------------
    usr4.set("id", "shengongbao")
    usr4.set("name", "申公豹")
    usr4.set("hp", 100);
    usr4.set("defense", 5);
    usr4.setTemp("rdIdx", 2);

    usr3.set("rdSkills", [
        100001, 100002
    ]);


    usr4.set("rdSkills", [
        100068, 100076
    ]);


    me.fightingCamps = [];


    let camp1 = {
        name: "阵营1",
        killers: [usr, usr2],
        //1等待战斗 2战斗中 3战斗完成
        stat: 1,
        //该谁出手
        idx: 0,

    };

    let camp2 = {
        name: "阵营2",
        killers: [usr3, usr4],
        stat: 1,
        idx: 0,


    };

    camp1.enemyCamp = camp2;
    camp2.enemyCamp = camp1;


    me.combatGroupQue = [];

    //创建队列

    let que1 = {
        name: "队列名字-通常以激活战斗的玩家的名字为主",
        camps: [camp1, camp2],
        stat: FIX_STAT.QUE.WAIT,
        idx: 0,
        round: {p: 1, c: 1},
    }

    me.combatGroupQue.push(que1)


}

function heartbeat() {
    os.debug("heartbeat")
    let me = thisObject();
    if (!me.hasInit) {
        me.hasInit = true;
        os.debug(me);
        me.init();
        return;
    }


    for (let que in me.combatGroupQue) {//处理所有队列


        let q = me.combatGroupQue[que];


        let kCamp = me.getNeedCombatCampKiller(q);
        let vCamp = me.getNeedCombatCampKiller(q);

        let killer = me.getKiller(kCamp);
        let victim = me.getKillerRandom(vCamp);

        if (!victim || !killer) {
            //没有阵营可以战斗了..
            //其中一方的人员全部没有血量了
            let combatResult = me.getWinAndLose(kCamp, vCamp);
            if (combatResult == 1) {
                //重设出招状态
                q.round.p = q.round.c;
                q.round.c++;
                me.resetAttacks(kCamp, vCamp);
                return;
            }
            os.debug(combatResult.win.name + "胜利了");
            continue;
        }
        if (q.round.c == 1 || q.round.c - q.round.p == 1) {
            os.debug("第" + q.round.c + "回合");
        }
        os.debug(killer);
       // saveObject(killer);
        //
        // os.debug( restoreObject(killer) );


        // os.debug( killer.get("name"), "杀手信息");
        // os.debug( victim.get("name"), "受害者信息");
        me.combat(killer, victim);

    }


}




